<?php
// 检查是否已经安装
if (file_exists(__DIR__ . '/config/config.php')) {
    header('Location: index.php');
    exit;
}

// 处理安装表单提交
if ($_POST) {
    $admin_username = $_POST['admin_username'] ?? '';
    $admin_password = $_POST['admin_password'] ?? '';
    $api_key = $_POST['api_key'] ?? '';
    $base_url = $_POST['base_url'] ?? '';
    $default_model = $_POST['default_model'] ?? '';
    $custom_model = $_POST['custom_model'] ?? '';

    // 验证输入
    if (empty($admin_username) || empty($admin_password) || empty($api_key) || empty($base_url)) {
        $error = '所有字段都是必填的';
    } else {
        // 选择模型 - 如果选择了自定义模型，则使用自定义模型
        $model = $default_model === 'custom' ? $custom_model : $default_model;
        
        if (empty($model)) {
            $error = '请选择或输入AI模型';
        } else {
            // 创建配置文件
            $config_content = "<?php\n";
            $config_content .= "\$config = [\n";
            $config_content .= "    'api_key' => '" . addslashes($api_key) . "',\n";
            $config_content .= "    'base_url' => '" . addslashes($base_url) . "',\n";
            $config_content .= "    'default_model' => '" . addslashes($model) . "',\n";
            $config_content .= "];\n";
            
            file_put_contents(__DIR__ . '/config/config.php', $config_content);
            
            // 创建管理员账户
            $admin_data = [
                'username' => $admin_username,
                'password' => password_hash($admin_password, PASSWORD_DEFAULT)
            ];
            
            file_put_contents(__DIR__ . '/config/admin.php', "<?php\n\$admin = " . var_export($admin_data, true) . ";\n");
            
            // 重定向到首页
            header('Location: index.php');
            exit;
        }
    }
}

// 预设模型列表
$models = [
    'tstars2.0',
    'qwen3-coder-plus',
    'deepseek-v3.1',
    'deepseek-r1',
    'kimi-k2-0905',
    'qwen3-235b',
    'custom'
];
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>lanxAI - 首个览星自研发AI - 安装</title>
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>
    <div class="install-container">
        <h1>lanxAI - 首个览星自研发AI</h1>
        <h2>安装向导</h2>
        
        <?php if (isset($error)): ?>
            <div class="error"><?php echo $error; ?></div>
        <?php endif; ?>
        
        <form method="post">
            <div class="form-group">
                <label for="admin_username">管理员用户名</label>
                <input type="text" id="admin_username" name="admin_username" required>
            </div>
            
            <div class="form-group">
                <label for="admin_password">管理员密码</label>
                <input type="password" id="admin_password" name="admin_password" required>
            </div>
            
            <div class="form-group">
                <label for="api_key">API Key</label>
                <input type="text" id="api_key" name="api_key" required>
            </div>
            
            <div class="form-group">
                <label for="base_url">Base URL</label>
                <input type="text" id="base_url" name="base_url" value="https://apis.iflow.cn/v1" required>
                <small>默认值为: https://apis.iflow.cn/v1</small>
            </div>
            
            <div class="form-group">
                <label for="default_model">选择AI模型</label>
                <select id="default_model" name="default_model" onchange="toggleCustomModel()">
                    <?php foreach ($models as $model): ?>
                        <option value="<?php echo $model; ?>" <?php echo $model === 'gpt-3.5-turbo' ? 'selected' : ''; ?>>
                            <?php echo $model === 'custom' ? '自定义模型' : $model; ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            
            <div class="form-group custom-model-container" id="custom_model_container">
                <label for="custom_model">自定义模型名称</label>
                <input type="text" id="custom_model" name="custom_model" placeholder="输入自定义模型名称">
            </div>
            
            <button type="submit" class="btn">完成安装</button>
        </form>
        
        <div class="footer">
            LANX开源AI
        </div>
    </div>
    
    <script>
        function toggleCustomModel() {
            const modelSelect = document.getElementById('default_model');
            const customContainer = document.getElementById('custom_model_container');
            
            if (modelSelect.value === 'custom') {
                customContainer.style.display = 'block';
            } else {
                customContainer.style.display = 'none';
            }
        }
        
        // 初始化时检查是否选择了自定义模型
        window.onload = function() {
            toggleCustomModel();
        };
    </script>
</body>
</html>
